﻿var SPACECHAR=" ";
var CodeList=CodeList.split(',');// CodeList string lay tu file data-utf.js
var LENGTH = CodeList.length;//30012 word
var CodeArr = initCodeArr();
var CurrentWord = new Array(2);
var Total = 0;
var Sum = 0;


function initCodeArr() {
	var CodeArr = new Array(length);
	for(var i=0; i<LENGTH; i++) {
		var item = CodeList[i];
		var spaceIndex = item.indexOf(SPACECHAR);
                if(spaceIndex>=0){
			var data = new Array(2);
			data[0] = item.substring(0,spaceIndex);
			data[1] = item.substring(spaceIndex+1,item.length);
			CodeArr[i] = data;
		}
	}
	return CodeArr;
}

// Luu tu moi vao CurrentWord, va Hien thi tu moi vao newWord placehold
function getNewWord() {
	var random = Math.floor(Math.random()*LENGTH);
	CurrentWord = CodeArr[random];
	setTextContent(WORD.NewHold, CurrentWord[0]);
	//lay translate tu google
	var url = "trans.php?input=" + CurrentWord[0];
	loadXMLDoc(url);
}
function Trim(sString)
{
	while (sString.substring(0,1) == " ")
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == " ")
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function submitAndGet() {
	WORD.InputHold.onkeypress = function(e){
    		if (!e) e = window.event;   // resolve event instance
		if (e.keyCode == '13'){

			//1. Cham ket qua,so sanh tra loi voi dap an			
			var strAnswer = Trim(this.value);
			if (strAnswer != CurrentWord[1]) {
				setTextContent(WORD.FailHold, strAnswer);
			} else {
				setTextContent(WORD.FailHold, "");
				Sum++;
			}
			Total++;
			
			//2. Hien thi cham ket qua
			setTextContent(WORD.OldHold, CurrentWord[0]);
			setTextContent(WORD.CorrectHold, CurrentWord[1]);
			setTextContent(WORD.SumHold, Sum + "/" + Total);
			WORD.InputHold.value="";
			setTextContent(WORD.TransCnHold, "");
			setTextContent(WORD.TransViHold, "");

			//3.Hien thi tu moi vao randomWord va  Luu tu moi vao CurrentWord
			getNewWord(); 		

      			return false;
    		}
  	}
}

function loadXMLDoc(url)
{
	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
  	}
	else
	{// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	xmlhttp.onreadystatechange=function(){
  		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			var trans = xmlhttp.responseText.split("|");
			if (trans.indexOf('?') == -1) {// tranh loi IE khong hien duoc, chi hien dau ? chua biet why
				setTextContent(WORD.TransCnHold, trans[0]);
				setTextContent(WORD.TransViHold, trans[1]);
			}
			//var objData = eval('(' + trans + ')'); //trans.parseJSON();			
    		}
  	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}

/*
Hàm thay cho innerText, để có thể chạy trên mọi trình duyệt
*/
function setTextContent(element, text) {
    while (element.firstChild!==null)
        element.removeChild(element.firstChild); // remove all existing content
    element.appendChild(document.createTextNode(text));
}

/*
Hàm chuyển từ kiểu pinyin thật sự sang kiểu kí hiệu pinyin bằng số
 them van "ing" va "v" so voi ban goc trang chinese-tools
Ham nay hien tai chua dung den
*/
function untranscrire(car) {

car = car.replace(/ūn/g, "un1");
car = car.replace(/ún/g, "un2");
car = car.replace(/ǔn/g, "un3");
car = car.replace(/ùn/g, "un4"); 
car = car.replace(/ér/g, "er2");
car = car.replace(/ěr/g, "er3");
car = car.replace(/èr/g, "er4"); 
car = car.replace(/āo/g, "aō");           
car = car.replace(/áo/g, "aó");
car = car.replace(/ǎo/g, "aǒ");
car = car.replace(/ào/g, "aò");
car = car.replace(/ōu/g, "oū");           
car = car.replace(/óu/g, "oú");
car = car.replace(/ǒu/g, "oǔ");
car = car.replace(/òu/g, "où");
car = car.replace(/āi/g, "aī");
car = car.replace(/ái/g, "aí");
car = car.replace(/ǎi/g, "aǐ");
car = car.replace(/ài/g, "aì"); 
car = car.replace(/ēi/g, "eī");
car = car.replace(/éi/g, "eí");
car = car.replace(/ěi/g, "eǐ");
car = car.replace(/èi/g, "eī");

car = car.replace(/ōng/g, "ong1");
car = car.replace(/óng/g, "ong2");
car = car.replace(/ǒng/g, "ong3");
car = car.replace(/òng/g, "ong4");

car = car.replace(/īng/g, "ing1");
car = car.replace(/íng/g, "ing2");
car = car.replace(/ǐng/g, "ing3");
car = car.replace(/ìng/g, "ing4");

car = car.replace(/īn/g, "in1");
car = car.replace(/ín/g, "in2");
car = car.replace(/ǐn/g, "in3");
car = car.replace(/ìn/g, "in4");

car = car.replace(/ēng/g, "eng1");
car = car.replace(/éng/g, "eng2");
car = car.replace(/ěng/g, "eng3");
car = car.replace(/èng/g, "eng4");

car = car.replace(/ēn/g, "en1");
car = car.replace(/én/g, "en2");
car = car.replace(/ěn/g, "en3");
car = car.replace(/èn/g, "en4");

car = car.replace(/āng/g, "ang1");
car = car.replace(/áng/g, "ang2");
car = car.replace(/ǎng/g, "ang3");
car = car.replace(/àng/g, "ang4");

car = car.replace(/ān/g, "an1");
car = car.replace(/án/g, "an2");
car = car.replace(/ǎn/g, "an3");
car = car.replace(/àn/g, "an4");

car = car.replace(/ā/g, "a1");
car = car.replace(/á/g, "a2");
car = car.replace(/ǎ/g, "a3");
car = car.replace(/à/g, "a4"); 
car = car.replace(/ē/g, "e1");
car = car.replace(/é/g, "e2");
car = car.replace(/ě/g, "e3");
car = car.replace(/è/g, "e4");     
car = car.replace(/ī/g, "i1");
car = car.replace(/í/g, "i2");
car = car.replace(/ǐ/g, "i3");
car = car.replace(/ì/g, "i4"); 
car = car.replace(/ō/g, "o1");           
car = car.replace(/ó/g, "o2");
car = car.replace(/ǒ/g, "o3");
car = car.replace(/ò/g, "o4");
car = car.replace(/ū/g, "u1");
car = car.replace(/ú/g, "u2");
car = car.replace(/ǔ/g, "u3");
car = car.replace(/ù/g, "u4"); 
car = car.replace(/ǖ/g, "ü1");     
car = car.replace(/ǘ/g, "ü2");
car = car.replace(/ǚ/g, "ü3");
car = car.replace(/ǜ/g, "ü4"); 
car = car.replace(/ǖ/g, "v1");     
car = car.replace(/ǘ/g, "v2");
car = car.replace(/ǚ/g, "v3");
car = car.replace(/ǜ/g, "v4");

//document.conversion.saisie.value=car;

return car;
}


function BodyOnLoad() {
	WORD = {
	NewHold: document.getElementById("newWord"),
	OldHold:      document.getElementById("oldWord"),
	FailHold:      document.getElementById("failAnswer"),
	CorrectHold:      document.getElementById("correctAnswer"),
	TransCnHold:      document.getElementById("transCn"),
	TransViHold:      document.getElementById("transVi"),
	InputHold: document.getElementById("saisie"),
	SumHold: document.getElementById("sum")
	};
	getNewWord();
	submitAndGet();

}